package com.ygqh.baby.controller.mobile;

import com.ygqh.baby.annotation.YgController;
import com.ygqh.baby.ao.PromotionType;
import com.ygqh.baby.ao.ResponseEntity;
import com.ygqh.baby.ao.SalesPlatform;
import com.ygqh.baby.model.BmdismModel;
import com.ygqh.baby.model.YgPromotionItemModel;
import com.ygqh.baby.model.YgPromotionModel;
import com.ygqh.baby.model.YgPromotionProductModel;
import com.ygqh.baby.service.YgBmdismService;
import com.ygqh.baby.service.YgProductActionGiftService;
import com.ygqh.baby.service.YgPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 促销相关
 *
 * @author guohao
 */
@Controller
@RequestMapping("/app/promotion")
public class YgPromotionController extends BaseController {

    @Autowired
    private YgPromotionService promotionService;
    @Autowired
    private YgBmdismService ygBmdismService;
    @Autowired
    private YgProductActionGiftService ygProductActionGiftService;

    /**
     * 获取促销商品列表
     *
     * @param pType
     * @return
     */
    @RequestMapping(value = "findPromotionItemList")
    @ResponseBody
    public ResponseEntity findPromotionItemList(PromotionType pType, BigDecimal orderPrice) {
        List<YgPromotionItemModel> promotionList;
        if (pType.equals(PromotionType.JiaJiaGou)) {
            orderPrice = null;
        }
        promotionList = promotionService.findPromotionItemList(pType, orderPrice, SalesPlatform.Yijiadian);
        return ResponseEntity.success(promotionList);
    }

    /**
     * 获取优惠信息
     *
     * @return
     */
    @RequestMapping(value = "findOwnPromotionList")
    @ResponseBody
    public ResponseEntity findOwnPromotionList() {
        Map<String, Object> map = ownPromotionListSegment();
        return ResponseEntity.success(map);
    }

    /**
     * 查找加价购列表
     *
     * @return
     */
    @RequestMapping(value = "findJiajiagoPromotionList")
    @ResponseBody
    public ResponseEntity findJiajiagoPromotionList(Long productId) {
        List<YgPromotionModel> list = promotionService.findJiajiagoPromotionList(productId);
        return ResponseEntity.success(list);
    }

    @RequestMapping(value = "findBmdismByProductIds")
    @ResponseBody
    public ResponseEntity findBmdismByProductIds(Long... productIds) {
        List<BmdismModel> list = ygBmdismService.findByProductIds(productIds);
        return ResponseEntity.success(list);
    }

    /**
     * 获取活动信息
     *
     * @return
     */
    @RequestMapping(value = "findOwnPromotionListNotLogin")
    @YgController
    @ResponseBody
    public ResponseEntity findOwnPromotionListNotLogin(Long productId, Long brandId) {
        Map<String, Object> map = promotionService.findOwnPromotionListNotLogin(productId, brandId, SALES_PLATFORM);
        if (map.isEmpty()) {
            return ResponseEntity.success(null);
        }
        return ResponseEntity.success(map);
    }

    private Map<String, Object> ownPromotionListSegment() {
        List<YgPromotionModel> promotionList = promotionService.findOwnPromotionList(null, SALES_PLATFORM);
        Map<String, Object> map = new HashMap<>();
        if (promotionList != null && promotionList.size() > 0) {
            for (YgPromotionModel model : promotionList) {
                if (model.getPromotionType().equals(PromotionType.JiaJiaGou)) {
                    map.put("JiaJiaGou", model);
                }
                if (model.getPromotionType().equals(PromotionType.ManJian)) {
                    map.put("ManJian", model);
                }
            }
        }
        return map;
    }

    @RequestMapping(value = "getSingleGiftList")
    @ResponseBody
    public ResponseEntity getSingleGift(Long... productIds) {
        List<YgPromotionProductModel> list = ygProductActionGiftService.getSingleGiftList(productIds);
        return ResponseEntity.success(list);
    }
}
